﻿////////////////////////////////////////////////////////////////////////////////////////////////////
// file:	DBConnection\DBConnect.cs
//
// summary:	Implements the database connect class
////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Usermanagment.VisualDisplay.UnderTheHood;
using MySql.Data.MySqlClient;
using System.Net;
using System.Net.Sockets;
namespace Usermanagment.DBConnection
{
   ////////////////////////////////////////////////////////////////////////////////////////////////////
   /// <summary>	Database connect. </summary>
   ///
   /// <remarks>	Avishai, 10/09/2012. </remarks>
   ////////////////////////////////////////////////////////////////////////////////////////////////////

   public class DBConnect
    {
       /// <summary>	The connection. </summary>
       private MySqlConnection connection;
       /// <summary>	The server. </summary>
       private string server;
       /// <summary>	The username. </summary>
       private string username;
       /// <summary>	The password. </summary>
       private string password;
       /// <summary>	The database. </summary>
       private string database;
       /// <summary>	The con string. </summary>
       private string conString;
       /// <summary>	The is connected. </summary>
       private Boolean init = false ,isConnected = false;
	   /// <summary>	The message. </summary>
	   private String message;
	   private String playerTable, statsTable;

	   public DBConnect()  
	   {
		  ServerData sd = (new SerializeDat().work());
		  if (sd != null)
		  {
			  this.server = sd.sAddress;
			  this.database = sd.sDatabase;
			  this.playerTable = sd.sPlayersdb;
			  this.statsTable = sd.sStatsdb;
		  }
		  else 
		  {
			  foreach (var addr in Dns.GetHostEntry("finalprojakoe.servegame.com").AddressList)
			  {
				  if (addr.AddressFamily == AddressFamily.InterNetwork)
				  {
					  this.server = addr + "";
					  break;
				  }
			  }
			  this.database = "players";
			  this.statsTable = "playerstat";
			  this.playerTable = "playersdb";
		  }
		   this.username = "useract";
		  this.password = "Logmein@1";
	   }
	   public DBConnect(ServerData sd)
	   {
		   if (sd != null)
		   {
			   this.server = sd.sAddress;
			   this.username = sd.sUser;
			   this.password = sd.sPassword;
			   this.database = sd.sDatabase;
			   this.playerTable = sd.sPlayersdb;
			   this.statsTable = sd.sStatsdb;
		   }
	   }
       ////////////////////////////////////////////////////////////////////////////////////////////////////
       /// <summary>	Gets or sets the Server Address. </summary>
       ///
       /// <value>	The Address. </value>
       ////////////////////////////////////////////////////////////////////////////////////////////////////
	   public string Sserver
       {
           get { return server; }
           set { server = value; init = false; }
       }

       ////////////////////////////////////////////////////////////////////////////////////////////////////
       /// <summary>	Gets or sets the username. </summary>
       ///
       /// <value>	The username. </value>
       ////////////////////////////////////////////////////////////////////////////////////////////////////

       public string User
       {
           get { return username; }
           set { username = value; init = false; }
       }
	   public string playerDB
	   {
		   get { return this.playerTable; }
	   }
	   public string playerStats
	   {
		   get { return this.statsTable; }
		  
	   }
       ////////////////////////////////////////////////////////////////////////////////////////////////////
       /// <summary>	Gets or sets the password. </summary>
       ///
       /// <value>	The password. </value>
       ////////////////////////////////////////////////////////////////////////////////////////////////////

       public string Password
       {
           get { return password; }
           set { password = value; init = false; }
       }

       ////////////////////////////////////////////////////////////////////////////////////////////////////
       /// <summary>	Gets or sets the database. </summary>
       ///
       /// <value>	The database. </value>
       ////////////////////////////////////////////////////////////////////////////////////////////////////

       public string Database
       {
           get { return database; }
           set { database = value; init = false; }
        }

       ////////////////////////////////////////////////////////////////////////////////////////////////////
       /// <summary>	Initializes this object. </summary>
       ///
       /// <remarks>	Avishai, 10/09/2012. </remarks>
       ////////////////////////////////////////////////////////////////////////////////////////////////////

       private void initialize()
       {
           conString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + username + ";"
               + "PASSWORD=" + password + ";" ;
           init = true;
       }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>	Opens the connection. </summary>
        ///
        /// <remarks>	Avishai, 10/09/2012. </remarks>
        ///
        /// <returns>	true if it succeeds, false if it fails. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public Boolean OpenConnection()
       {

		   try
		   {
			   if (!init)
				   initialize();
			   connection = new MySqlConnection(conString);
			  
			   connection.Open();
			   isConnected = true;
			   message = "Connected!";
			   return true;

		   }
		   catch (MySqlException ex)
		   {
			   switch (ex.Number)
			   {
				   case 0:
					   message = "Cant Connect to server";
					   break;
				   case 1045:
					   message = "Bad username/Password";
					   break;
				   default:
					   message = ex.Message;
					   break;
			   }
			   return false;

		   }
		   catch (Exception ex)
		   {
			   message = ex.Message;
			   return false;
		   }

       }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>	Closes the connection. </summary>
        ///
        /// <remarks>	Avishai, 10/09/2012. </remarks>
        ///
        /// <returns>	true if it succeeds, false if it fails. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public Boolean closeConnection()
        {

            try
            {
                if (isConnected)
                {
                    connection.Close();
                    isConnected = false;
					message = "Disconnected";
                }
                return true;
            }
            catch (System.Exception ex)
            {
               message = (ex.Message);
                return false;
            }
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>	Is it connected. </summary>
        ///
        /// <remarks>	Avishai, 10/09/2012. </remarks>
        ///
        /// <returns>	true if it Connected, false if it's not. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public Boolean Connected()
        {
            return isConnected;

        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>	Gets the Database Connection. </summary>
        ///
        /// <remarks>	Avishai, 10/09/2012. </remarks>
        ///
        /// <returns>	The connection. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public MySqlConnection getCon()
        {
            return connection;
        }

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the message. </summary>
		///
		/// <remarks>	Avishai, 10/09/2012. </remarks>
		///
		/// <returns>	The message. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public String getMsg()
		{
			return message;
		}

    }
}
